package com.topscore.uke.mvp.presenters;

import android.graphics.Color;

import com.topscore.uke.entities.MyPerformanceData;
import com.topscore.uke.entities.MyPerformanceSummaryData;
import com.topscore.uke.entities.MyPerformanceSummaryListBean;
import com.topscore.uke.entities.PerformanceChartsEntity;
import com.topscore.uke.entities.SalesRankingItem;
import com.topscore.uke.mvp.contractors.GetMyPerformanceContractor;
import com.topscore.uke.mvp.model.GetMyPerformanceModel;
import com.topscore.uke.network.BaseResponse;
import com.topscore.uke.utils.UserInfoManager;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.ComboLineColumnChartData;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.model.ValueShape;
import me.yokeyword.indexablerv.EntityWrapper;

/**
 * Created by 71468 on 2017/10/30.
 */

public class GetMyPerformanceDataPresenter implements GetMyPerformanceContractor,
        GetMyPerformanceContractor.Presenter {

    private View mView;
    private final String userId = UserInfoManager.getUserInfo().getLoginName();
    private Model mModel;

    public GetMyPerformanceDataPresenter(View view) {
        this.mView = view;
        this.mModel = new GetMyPerformanceModel(new CallBack() {
            @Override
            public void onGetChartDataSuccess(BaseResponse<MyPerformanceData> response) {
                getRankListData("money");
                MyPerformanceData data = response.getData();
//                mView.onGetChartDataSuccess(getLineColumnData(data.getSaleMoneys(),data.getSaleCounts()));
//                mView.onGetTotalReturnAmountSuccess(data.getReturnMoneyTotal());
//                mView.onGetTotalReturnCountSuccess(data.getReturnCountTotal());
//                mView.onGetTotalSalesAmountSuccess(data.getSalesMoneyTotal());
//                mView.onGetTotalSalesCountSuccess(data.getSalesCountTotal());
            }

            @Override
            public void onGetSummarySuccess(BaseResponse<MyPerformanceSummaryData> response) {
                MyPerformanceSummaryData data = response.getData();
                mView.onGetChartDataSuccess(getLineColumnData(response.getData().getDetails()));
                DecimalFormat decimalFormat = new DecimalFormat("0.00");
                mView.onGetTotalReturnAmountSuccess(decimalFormat.format(data.getRefundAmount()));
                mView.onGetTotalReturnCountSuccess(String.valueOf(data.getRefundCount()));
                mView.onGetTotalSalesAmountSuccess(decimalFormat.format(data.getSalesAmount()));
                mView.onGetTotalSalesCountSuccess(String.valueOf(data.getSalesCount()));
            }

            @Override
            public void onGetRankListDataSuccess(BaseResponse<List<SalesRankingItem>> response) {
//                mView.onGetRankListSuccess(response.getData());
                mView.getRankingAdapter().setNewData(response.getData());
                mView.getRankingAdapter().loadMoreComplete();
                mView.getRankingAdapter().loadMoreEnd();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                mView.onFail(errorCode, errorMessage);
            }

            @Override
            public void onComplete() {
                mView.onLoadFinished();
            }
        });
    }

    private ComboLineColumnChartData getLineColumnData(List<MyPerformanceSummaryListBean> details) {
        ComboLineColumnChartData data = new ComboLineColumnChartData(getAmountChartData(details), getCountsChartData(details));
        List<AxisValue> axisXValues = new ArrayList<>();
//        for (int i = 0; i < details.size(); i++) {
//            String day = details.get(i).getDay();
//            AxisValue axisValue = new AxisValue(i).setLabel(day);
//            axisXValues.add(axisValue);
////            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
////            try {
////                Date date = dateFormat.parse(day);
////                AxisValue axisXValue = new AxisValue(i).setLabel(new SimpleDateFormat("MM-dd").format(date));
////                axisXValues.add(axisXValue);
////            } catch (ParseException e) {
////                e.printStackTrace();
////            }
//
//        }
        Axis axisX = new Axis(axisXValues);
        axisX.setMaxLabelChars(3);
        axisX.setTextColor(Color.parseColor("#131313"));
        axisX.setTextSize(14);
        axisX.setLineColor(Color.parseColor("#CE3B3E"));
        data.setAxisXBottom(axisX);
        return data;
    }

//    private ComboLineColumnChartData getLineColumnData(List<PerformanceChartsEntity> salesAmountEntities, List<PerformanceChartsEntity> salesCountsEntities) {
//        ComboLineColumnChartData data
//                = new ComboLineColumnChartData(getAmountChartData(salesAmountEntities),
//                getCountsChartData(salesCountsEntities));
//        List<AxisValue> axisXValues = new ArrayList<>();
//        for (int i = 0; i < salesAmountEntities.size(); i++) {
//            String day = salesAmountEntities.get(i).getX();
//            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//            try {
//                Date date = dateFormat.parse(day);
//                AxisValue axisXValue = new AxisValue(i).setLabel(new SimpleDateFormat("MM-dd").format(date));
//                axisXValues.add(axisXValue);
//            } catch (ParseException e) {
//                e.printStackTrace();
//            }
//
//        }
//        Axis axisX = new Axis(axisXValues);
//        axisX.setMaxLabelChars(3);
//        axisX.setTextColor(Color.parseColor("#131313"));
//        axisX.setTextSize(14);
//        data.setAxisXBottom(axisX);
//        return data;
//    }

    private LineChartData getCountsChartData(List<MyPerformanceSummaryListBean> details) {
        List<PointValue> pointValues = new ArrayList<>();
        for (int i = 0; i < details.size(); i++) {
            PointValue pointValue = new PointValue(i, details.get(i).getSalesCount());
            pointValues.add(pointValue);
        }
        Line line = new Line(pointValues);
        line.setColor(Color.parseColor("#f8b551"));
        line.setPointRadius(3);
        line.setStrokeWidth(1);
        line.setShape(ValueShape.CIRCLE);
        line.setHasLines(true);
        line.setHasPoints(true);
        line.setCubic(false);
        List<Line> lines = new ArrayList<>();
        lines.add(line);
        LineChartData data = new LineChartData(lines);
//        data.setAxisXBottom(new Axis(axisXValues).setMaxLabelChars(4));
        return data;
    }


//    private LineChartData getCountsChartData(List<PerformanceChartsEntity> salesCountsEntities) {
//            List<PointValue> pointValues = new ArrayList<>();
//            for (int i = 0; i < salesCountsEntities.size(); i++) {
//                PointValue pointValue = new PointValue(i, salesCountsEntities.get(i).getY());
//                pointValues.add(pointValue);
//            }
//            Line line = new Line(pointValues);
//            line.setColor(Color.parseColor("#f8b551"));
//            line.setPointRadius(3);
//            line.setStrokeWidth(1);
//            line.setShape(ValueShape.CIRCLE);
//            line.setHasLines(true);
//            line.setHasPoints(true);
//            line.setCubic(false);
//            List<Line> lines = new ArrayList<>();
//            lines.add(line);
//            LineChartData data = new LineChartData(lines);
//    //        data.setAxisXBottom(new Axis(axisXValues).setMaxLabelChars(4));
//            return data;
//        }

    private ColumnChartData getAmountChartData(List<MyPerformanceSummaryListBean> details) {
        List<Column> columns = new ArrayList<>();
        for (int i = 0; i < details.size(); i++) {
            List<SubcolumnValue> subcolumnValues = new ArrayList<>();
            SubcolumnValue subcolumnValue = new SubcolumnValue((float) details.get(i).getSalesAmount());
            subcolumnValue.setColor(Color.parseColor("#CE3B3E"));
            subcolumnValues.add(subcolumnValue);
            columns.add(new Column(subcolumnValues));
        }
        ColumnChartData data = new ColumnChartData(columns);
        return data;
    }

//        private ColumnChartData getAmountChartData(List<PerformanceChartsEntity> salesAmountEntities) {
//        List<Column> columns = new ArrayList<>();
//        for (int i = 0; i < salesAmountEntities.size(); i++) {
//            List<SubcolumnValue> columnValues = new ArrayList<>();
//            SubcolumnValue subcolumnValue = new SubcolumnValue(salesAmountEntities.get(i).getY());
//            subcolumnValue.setColor(Color.parseColor("#CE3B3E"));
//            columnValues.add(subcolumnValue);
//            columns.add(new Column(columnValues));
//        }
//        ColumnChartData data = new ColumnChartData(columns);
//        return data;
//    }

    @Override
    public void getMyPerformanceData() {
        mView.onLoading("正在获取业绩数据");
        mModel.getPerformanceData(userId);
    }

    @Override
    public void getRankListData(String sortName) {
        mView.onLoading("正在获取销售榜单");
        mModel.getSalesRankList(userId, sortName);
    }

    @Override
    public void getSalesData() {
        mView.onLoading("正在获取数据");
        mModel.getData(userId);
    }
}
